package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Latest;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.LatestService;
import cn.edu.cimv.service.UploadService;
import cn.edu.cimv.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by 门铎 on 2016/8/2.
 */
@Controller
@RequestMapping("/cimv/latest")
public class LatestController {
    @Autowired
    private LatestService latestService;
    @Autowired
    private Latest latest;
    @Autowired
    private Message message;

    @Autowired private UploadService uploadService;
    @RequestMapping("list")
    public String list(Model model){
        model.addAttribute("latestList",latestService.queryLatest());
        return "latest/list";
    }
    @RequestMapping("new")
    public String latestNew(){
        return "latest/new";
    }

    @RequestMapping( value = "create", method = RequestMethod.POST)
    public @ResponseBody Message create(@RequestParam("editor") String editor,
                                        @RequestParam("title") String title,
                                        @RequestParam(value = "pic",defaultValue = "") String pic) {
        latest.setDate(DateUtil.toDateString(new Date()));
        latest.setContent(editor);
        latest.setTitle(title);
        latest.setPic(pic);
        latestService.editLatest(latest);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        latestService.deleteLatest(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("id")int id,Model model){
        model.addAttribute("latest",latestService.queryLatestById(id));
        return "latest/update";
    }
    @RequestMapping(value = "updateLatest/{id}",method = RequestMethod.POST)
    public @ResponseBody Message updateNews(@PathVariable("id")int id,
                                            @RequestParam("title")String title,
                                            @RequestParam("editor") String editor,
                                            @RequestParam(value = "pic",defaultValue = "") String pic){
        //修改的内容
        latest.setId(id);
        latest.setDate(DateUtil.toDateString(new Date()));
        latest.setContent(editor);
        latest.setTitle(title);
        latest.setPic(pic);
        //更新
        latestService.updateLatest(latest);

        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;

    }

    @RequestMapping(value = "pic" ,method = RequestMethod.POST)
    public @ResponseBody String uploadPic(@RequestParam(value = "pic",required = true)MultipartFile pic,HttpServletRequest request){
        //上传图片
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String picName = uploadService.uploadPic(pic,rootPath);
        return picName;
    }
}
